//
//  CostomPaiXuPickerView.m
//  YaoHeAPP
//
//  Created by xyh on 16/6/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CustomPaiXuPickerView.h"

@interface CustomPaiXuPickerView () <UIPickerViewDataSource, UIPickerViewDelegate> {
    NSArray *_pickerArray;
    int _pickerRow;
}

@property (nonatomic, strong) UILabel *tipLabel;                // 提示label
@property (nonatomic, strong) UIPickerView *pickerView;         // 时间选择器datePicker
@property (nonatomic, strong) UIButton *cancelButton;           // 取消button
@property (nonatomic, strong) UIButton *conformButton;          // 确认button

@end

@implementation CustomPaiXuPickerView
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 提示label
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.backgroundColor = [UIColor blackColor];
        tipLabel.font = Font(14);
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.text = @"选择排序方式";
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.frame = CGRectMake(0, 0, self.width, 30);
        [self addSubview:tipLabel];
        self.tipLabel = tipLabel;
        
        // 时间选择器
        _pickerArray = [NSArray arrayWithObjects:@"按时间从高到低",@"按时间从低到高",@"按成单数从低到高",@"按成单数从高到低", nil];
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, tipLabel.bottom, self.width, 216)];
        pickerView.delegate=self;
        pickerView.dataSource=self;
        [self addSubview:pickerView];
        self.pickerView = pickerView;
        
        _pickerRow = 0;
        
        UIView *fristLineView = [[UIView alloc] initWithFrame:CGRectMake(0, pickerView.bottom-5, self.width, 0.5)];
        fristLineView.backgroundColor = RGBCOLOR(230, 232, 241);
        [self addSubview:fristLineView];

        UIView *secondLineView = [[UIView alloc] initWithFrame:CGRectMake(self.width/2, pickerView.bottom-5, 0.5, 35)];
        secondLineView.backgroundColor = RGBCOLOR(230, 232, 241);
        [self addSubview:secondLineView];

        // 返回按钮
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton setTitle:@"返回" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.frame = CGRectMake(0, pickerView.bottom, self.width/2, 30);
        [self addSubview:cancelButton];
        self.cancelButton = cancelButton;
        
        // 确认按钮
        UIButton *conformButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [conformButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [conformButton setTitle:@"确认" forState:UIControlStateNormal];
        [conformButton addTarget:self action:@selector(conformAction) forControlEvents:UIControlEventTouchUpInside];
        conformButton.frame = CGRectMake(self.width/2, pickerView.bottom, self.width/2, 30);
        [self addSubview:conformButton];
        self.conformButton = conformButton;
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [_pickerArray count];
}

////返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
//- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return [_pickerArray objectAtIndex:row];
//}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        _pickerRow = (int)row;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view {
   UILabel *mycom1 = view ? (UILabel *) view : [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 30.0f)];
    mycom1.text = _pickerArray[row];
    mycom1.textAlignment = NSTextAlignmentCenter;
    mycom1.font = Font(13);
    mycom1.backgroundColor = [UIColor clearColor];
    
    return mycom1;
}

#pragma mark 取消操作
- (void)cancelAction {
    if (self.cancelActionBlock) {
        self.cancelActionBlock();
    }
}

#pragma mark 确认操作
- (void)conformAction {
    if (self.conformActionBlock) {
        self.conformActionBlock(_pickerRow);
    }
}

@end
